package com.it.gw.modules.station.adapter

import EquimentVoList
import android.annotation.SuppressLint
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexboxLayoutManager
import com.it.common.utils.DisplayUtil
import com.it.common.utils.RvSpacesItemUtils
import com.it.common.utils.isNotEmpty
import com.it.data.network.rsp.FileVo
import com.it.data.network.rsp.ProblemReportVo
import com.it.gw.R
import com.it.gw.databinding.ItemProblemBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.station.activity.GQStationReportActivity
import com.it.gw.modules.station.activity.PhotoViewActivity


class ProblemReportAdapter(
    var context: GQStationReportActivity,
    var listener: (ProblemReportVo) -> Unit,
    var datas: List<ProblemReportVo>
) : RecyclerView.Adapter<ProblemReportAdapter.ViewHolder>() {
    companion object {
        const val TAG = "ProblemReportAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemProblemBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        holder.setIsRecyclable(false)
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.name.text = data.operationType
        //问题上报
        if (data.needAssist == "1") {
            holder.binding.ivReport.setImageResource(R.drawable.icon_selected)
            holder.binding.ivDeal.setImageResource(R.drawable.icon_un_select)

        }
        //解决问题
        if (data.queDeal == "1") {
            holder.binding.ivDeal.setImageResource(R.drawable.icon_selected)
            holder.binding.llDealMethods.visibility = View.VISIBLE
            holder.binding.llDealFiles.visibility = View.VISIBLE
            holder.binding.llDealDesc.visibility = View.VISIBLE
        } else {
            holder.binding.ivDeal.setImageResource(R.drawable.icon_un_select)
            holder.binding.llDealMethods.visibility = View.GONE
            holder.binding.llDealFiles.visibility = View.GONE
            holder.binding.llDealDesc.visibility = View.GONE


        }


        holder.binding.llReport.setOnClickListener {
            if (data.needAssist == "1") {
                data.needAssist = "0"
            } else {
                data.needAssist = "1"
            }
            notifyDataSetChanged()

        }
        holder.binding.llDeal.setOnClickListener {
            if (data.queDeal == "1") {
                data.queDeal = "0"
            } else {
                data.queDeal = "1"
            }
            notifyDataSetChanged()
        }
        if (data.id == GQStationReportActivity.IS_CHECK_ID) {
            holder.binding.llReport.visibility = View.GONE
            holder.binding.llDeal.visibility = View.GONE
        } else {
            holder.binding.llReport.visibility = View.VISIBLE
            holder.binding.llDeal.visibility = View.VISIBLE
        }


        holder.binding.problemDesc.setText(data.problemDesc)
        holder.binding.problemDesc.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                data.problemDesc = s.toString()
            }

        })
        holder.binding.dealDesc.setText(data.dealDesc)
        holder.binding.dealDesc.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                data.dealDesc = s.toString()
            }

        })

        val map = hashMapOf(
            RvSpacesItemUtils.RIGHT_DECORATION to DisplayUtil.dip2px(context, 10f),
            RvSpacesItemUtils.BOTTOM_DECORATION to DisplayUtil.dip2px(context, 10f)
        )
        holder.binding.problemFiles.addItemDecoration(RvSpacesItemUtils(map))
        holder.binding.problemFiles.layoutManager = GridLayoutManager(context, 4)
        val problemFilesAdapter = SelectFileAdapter(context = context, datas = data.problemFiles)
        problemFilesAdapter.listener = {
            if (it.path.isNullOrEmpty()) {
                if (data.problemFiles.size == 6) {
                    Log.d(TAG, "onBindViewHolder: select file problemFiles.size==6")
                } else {
                    context.showPicSelect { type ->
                        if (type == 1) {
                            context.takePhoto {
                                Log.d(TAG, "onBindViewHolder: takePhoto ${it}")
                                data.problemFiles.add(FileVo(path = it!!, url = "", picId = ""))
                                problemFilesAdapter.notifyDataSetChanged()

                            }
                        } else if (type == 2) {
                            context.pickMedia {
                                Log.d(TAG, "onBindViewHolder: pickMedia ${it}")
                                data.problemFiles.add(FileVo(path = it!!, url = "", picId = ""))
                                problemFilesAdapter.notifyDataSetChanged()
                            }
                        }

                    }
                }

            } else {
                val intent = Intent(context, PhotoViewActivity::class.java)
                intent.putExtra("filePath", it.path)
                context.startActivity(intent)
            }

        }
        problemFilesAdapter.delete = {
            if (!it.path.isNullOrEmpty()) {
                data.problemFiles.remove(it)
                problemFilesAdapter.notifyDataSetChanged()
            }

        }
        holder.binding.problemFiles.adapter = problemFilesAdapter




        holder.binding.dealFiles.addItemDecoration(RvSpacesItemUtils(map))
        holder.binding.dealFiles.layoutManager = GridLayoutManager(context, 4)
        val dealFilesAdapter = SelectFileAdapter(context = context, datas = data.dealFiles)
        dealFilesAdapter.listener = {
            if (it.path.isNullOrEmpty()) {
                if (data.dealFiles.size == 6) {
                    Log.d(TAG, "onBindViewHolder: select file problemFiles.size==6")
                } else {
                    context.showPicSelect { type ->
                        if (type == 1) {
                            context.takePhoto {
                                Log.d(TAG, "onBindViewHolder: takePhoto ${it}")
                                data.dealFiles.add(FileVo(path = it!!, url = "", picId = ""))
                                dealFilesAdapter.notifyDataSetChanged()

                            }
                        } else if (type == 2) {
                            context.pickMedia {
                                Log.d(TAG, "onBindViewHolder: pickMedia ${it}")
                                data.dealFiles.add(FileVo(path = it!!, url = "", picId = ""))
                                dealFilesAdapter.notifyDataSetChanged()
                            }
                        }

                    }
                }

            } else {
                val intent = Intent(context, PhotoViewActivity::class.java)
                intent.putExtra("filePath", it.path)
                context.startActivity(intent)
            }

        }
        dealFilesAdapter.delete = {
            if (!it.path.isNullOrEmpty()) {
                data.dealFiles.remove(it)
                dealFilesAdapter.notifyDataSetChanged()
            }
        }


        holder.binding.dealFiles.adapter = dealFilesAdapter




        holder.binding.dealMethods.layoutManager = FlexboxLayoutManager(context)
        if (data.dealMethods.isNotEmpty()) {

            val equimentAdapter =
                EquimentAdapter(context = context, datas = data.equimentVos!!)
            equimentAdapter.leftListener = {

            }
            equimentAdapter.rigjtListener = {
                if (it?.type.isNotEmpty()) {
                    val model = "${it!!.type!!.split("_")[1]}"
                    context.showSelectModel(model = model, okAction = { dataCode, dataName ->
                        it.equipment = "${dataName}_${dataCode}"
                        equimentAdapter.notifyDataSetChanged()

                    })
                }
            }
            holder.binding.equiment.layoutManager = LinearLayoutManager(context)
            val map =
                hashMapOf(RvSpacesItemUtils.BOTTOM_DECORATION to DisplayUtil.dip2px(context, 6f))
            holder.binding.equiment.addItemDecoration(RvSpacesItemUtils(map))
            holder.binding.equiment.adapter = equimentAdapter


            val quaTypeShellAdapter =
                QuaTypeShellAdapter(context = context, datas = data.dealMethods!!)

            quaTypeShellAdapter.listener = {
                it.selected = !it.selected
                quaTypeShellAdapter.notifyDataSetChanged()
                if (it.selected) {
                    ProgressDialog.show(context)
                    context.equipment(it.id, resp = { d ->
                        ProgressDialog.hide()
                        if (d.data.isNotEmpty()) {
                            val vo = EquimentVoList(id = it.id, list = d.data)
                            data.equimentVos.add(vo)
                            equimentAdapter.notifyDataSetChanged()
                            if (data.equimentVos.isNotEmpty()) {
                                holder.binding.equiment.visibility = View.VISIBLE
                            } else {
                                holder.binding.equiment.visibility = View.GONE
                            }
                        }
                    })

                } else {
                    data.equimentVos.removeIf { e -> e.id == it.id }
                    equimentAdapter.notifyDataSetChanged()
                    if (data.equimentVos.isNotEmpty()) {
                        holder.binding.equiment.visibility = View.VISIBLE
                    } else {
                        holder.binding.equiment.visibility = View.GONE
                    }
                }


            }


            holder.binding.dealMethods.adapter = quaTypeShellAdapter




            if (data.queDeal == "1") {
                holder.binding.llDealMethods.visibility = View.VISIBLE
            } else {
                holder.binding.llDealMethods.visibility = View.GONE
            }


        } else {
            holder.binding.llDealMethods.visibility = View.GONE
        }

    }

    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemProblemBinding) :
        RecyclerView.ViewHolder(binding.root)
}